import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_chubao/route/app_route_name.dart';
import 'package:flutter_chubao/tools/app_image_load.dart';
import 'package:flutter_chubao/tools/app_colors.dart';
import 'package:flutter_chubao/tools/app_textstyle.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class AppWidgetProtocol extends StatefulWidget {
  // final String protocolName;
  // final List<String> organsIdArr;
  final Function(bool) clickProtocl;
  AppWidgetProtocol({
    // required this.organsIdArr,
    required this.clickProtocl,
  });
  @override
  AppWidgetProtocolState createState() => AppWidgetProtocolState();
}

class AppWidgetProtocolState extends State<AppWidgetProtocol> {
  bool isAgree = false;
  // {"key":'value',};
  List maps = [
    {
      'name': "注册协议",
      'url': 'https://miniapi.dev.cdzjzx.com/mini/protocol_html?file=register'
    },
    {
      'name': "隐私协议",
      'url': 'https://miniapi.dev.cdzjzx.com/mini/protocol_html?file=yinsi'
    }
  ];
  // {"code":200,"message":"ok","data":""};
  // Map products = {};
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        GestureDetector(
          onTap: () {
            // false
            if (mounted) {
              setState(() {
                isAgree = !isAgree;
              });
            }
            widget.clickProtocl.call(isAgree);
          },
          behavior: HitTestBehavior.opaque,
          // child: Row(
          //   children: [
          //     SSUtilImage.getIconByPath(
          //         isAgree ? 'icon_agree_s' : 'icon_agree_n',
          //         width: 16.w),
          //     SizedBox(
          //       width: 5.w,
          //     ),
          //     Text("我已阅读并同意 ",
          //         style: AppTextStyle.getNormalTextStyle(
          //             fontSize: 12, color: kColorTextB40)),
          //   ],
          // ),
          child: AppImage.getIconByPath(
              isAgree ? 'icon_agree_s' : 'icon_agree_n',
              width: 16),
        ),
        SizedBox(
          width: 5.w,
        ),
        Expanded(
            child: RichText(
                text: TextSpan(children: [
          TextSpan(
            text: "我已阅读并同意 ",
            style: AppTextStyle.getNormalTextStyle(
                fontSize: 12, color: kColorTextB40),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                if (mounted) {
                  setState(() {
                    isAgree = !isAgree;
                  });
                }
                widget.clickProtocl.call(isAgree);
              },
          ),
          ...maps
              .toList()
              .map((e) => TextSpan(
                  text: '《${e['name']}》',
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
// https://miniapi.dev.cdzjzx.com/mini/protocol_html?file=register
                      Get.toNamed(AppRouteName.appWebPage,
                          arguments: {'title': e['name'], 'url': e['url']});
                    },
                  style: AppTextStyle.getNormalTextStyle(
                      fontSize: 12, color: kColorAssistB5)))
              .toList()
        ]))),
      ],
    );
  }

  getProtocolRows() async {
    // SSHttpResponse response = await SSHttpClient.post(SSApis.protocolList,
    //     data: {'position': widget.protocolName});
    // if (response.ok) {
    //   final List<ModelProtocolEntity>? products =
    //       jsonConvert.convertListNotNull<ModelProtocolEntity>(response.data);
    //   if (products != null) {
    //     if (mounted) {
    //       setState(() {
    //         _products = products;
    //       });
    //     }
    //   }
    // }
    // for (var element in widget.organsIdArr) {
    //   if (maps.keys.toList().contains(element)) {
    //     products[element] = maps[element];
    //   }
    // }
  }

  @override
  void initState() {
    super.initState();
    getProtocolRows();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(AppWidgetProtocol oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
